home *** CD-ROM | disk | FTP | other *** search
- Path: news.lpr.carel.fi!usenet
- From: Ari Lukumies <aril@cmt.lpr.mail.carel.fi>
- Newsgroups: comp.lang.c
- Subject: Re: memory analysis tools where can I get them?
- Date: Mon, 19 Feb 1996 15:20:39 +0200
- Organization: Carelcomp Forest
- Message-ID: <31287927.25B0@cmt.lpr.mail.carel.fi>
- References: <rjv-1502961333560001@cdt.com>
- NNTP-Posting-Host: renoir.cclahti.carel.fi
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0b6a (WinNT; I)
-
- Ralph J. Verrilli wrote:
- >
- > does anyone know where I might be able to find tools to assist with memory
- > management. I am specifically looking for tools that might help with
- > finding memory leaks and memory overwrites.
- >
- > I'd like these tools be be relatively low cost since I need them to help
- > me finish my PhD. Thanks.
- >
- > Thanks.
- >
- > Please respond to: rjv@cdt.com
- >
- > rj
- >
-
- One very low-cost solution would be to write a homegrown management system. It would go
- something like this:
-
- #define malloc(x) MyMalloc(x, __FILE__, __LINE__)
- #define calloc(x, y) MyCalloc(x, y, __FILE__, __LINE__)
- #define free(x) MyFree(x, __FILE__, __LINE__)
-
- Now, you'd use these in all but one of your source modules, so that when you
- malloc/calloc/free memory, the functions being actually called are those of your own.
- Then, in one module (let's name it mymem.c), you will _not_ use these definitions but
- actually use the stdlib functions. Also, when allocating, you'll allocate four bytes more
- at the beginning of the memory and four after it, like this:
-
- void *MyMalloc(size_t size, char *file, long line)
- {
- char *ptr = malloc(size + 8);
-
- memset(ptr, '\xfd', 4);
- memset(ptr + size + 4, '\xfd', 4);
- return ptr + 4;
- }
-
- You can also use a global list of your own to keep track of the allocations, like:
-
- struct {
- char *file;
- long line;
- size_t size;
- void *ptr; /* data allocated */
- } Allocd;
-
- Now, when allocating, you can add an entry of this and set it up so when someone calls
- free, you can check whether the memory was overwritten:
-
- void MyFree(void *ptr, char *file, long line)
- {
- char *p = (char *)ptr - 4;
- int i;
-
- for (i = 0; i < NumAllocd; i++) {
- if ((ptr - 4) == Allocd[i].ptr) {
- /* Found this allocation */
- if (first_four_bytes != '\xfd' ||
- last_four_bytes != '\xfd)
- ; /* ERROR: MEMORY OVERWRITTEN ! */
- free(ptr);
- return;
- }
- }
- /* ERROR: No such memory allocation done! */
- }
-
- Etc. If you track the allocations using an array, you can add a function to call
- regularly that will step through the allocations and check if there has been memory
- overwrites. Also, you can check at desired points in your program, if the allocated
- memory has been freed that should have. When you have a memory overwrite, you can then
- (assuming you use a debugger cabable of it) turn a memory breakpoint on in the
- debugger and watch, where in your program some part overwrites the memory.
-
- This is just a hint towards the direction, but you'll get the idea.
-
- Happy memory hunting!
- AriL
- --
- All my opinions are mine and mine alone.
-